package fr.kirin.logger.i18n;

import java.util.Locale;
import java.util.ResourceBundle;


/**
 * 
 *This class manage I18N and provide message translation. 
 * @author kirin
 *
 */
public class I18N {

    private static ResourceBundle messages;
    private static Locale currentLocal;
    private static final String filePrefix = "resources/I18N/messages";

    static{
        currentLocal = new Locale("en","US");
        messages = ResourceBundle.getBundle(filePrefix, currentLocal);
    }

    private I18N(){}

    /**set locale and country like (en,US) or (fr,FR)
     * 
     * @param language language locale.
     * @param country country
     */
    public static void setLocale(String language, String country){
        currentLocal = new Locale(language,country);
        messages = ResourceBundle.getBundle(filePrefix, currentLocal);
    }
    
    /**set the local only with language.
     * 
     * @param language locale.
     */
    public static void setLocale(String language){
        currentLocal = new Locale(language);
        messages = ResourceBundle.getBundle(filePrefix, currentLocal);
    }

    /**get a message translation.
     * 
     * @param name the message
     * @return the message transaltion or identity if there is no translation available.
     */
    public static String getMessage(String name){
        try{
            return messages.getString(name);
        }catch (Exception e) {
            System.err.println("ATTENTION key "+name+" not found");
        }
        return name;
    }


}
